import { DeptVO } from '@/api/system/dept/types';
import { listDept } from '@/api/system/dept';
import {defineStore} from 'pinia';

const queryParams = ref({
  pageNum: 1,
  pageSize: 9999,
  deptName: undefined,
  deptCategory: undefined,
  status: undefined
});

export const useDeptStore = defineStore('dept', ()=>{
  const state = reactive({
    deptList: [] as DeptVO[]
  });
  const refreshDept = async () => {
    const res = await listDept(queryParams.value);
    state.deptList = [];
    state.deptList=res.data;
  };
  const getDept = (): DeptVO[] => {
    return state.deptList;
  }; 

  /**
   * 
   * @param id 
   * @returns dept information
   */

  const getInfoById = (id: number): DeptVO | undefined => { 
    const name = state.deptList.find(item => item.deptId === id);
    return state.deptList.find(item => item.deptId === id);
  }

  return {
    refreshDept,
    getDept,
    getInfoById
  }
});

export default useDeptStore;
